//
//  WFEmoticonKeyBoard.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/19.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

private let WFEmoticonKeyBoardCollectionViewCellID = "WFEmoticonKeyBoardCollectionViewCellID"
 var isChange:Bool = false
//自定义的表情键盘
class WFEmoticonKeyBoard: UIView {

    
    var formIndexPath:NSIndexPath?
    
    // MARK:  - 懒加载控件
    //定义表情视图
    private lazy var emoticonCollectionView:UICollectionView = {
    let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .Horizontal
    let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.pagingEnabled = true
        view.bounces = false
        view.backgroundColor = self.backgroundColor
        
        view.delegate = self
        view.dataSource = self
        
        view.registerClass(WFEmoticonCVCell.self, forCellWithReuseIdentifier: WFEmoticonKeyBoardCollectionViewCellID)
    
    return view
    }()
    
    //底部的toolbar
    private lazy var emojiToolBar:WFEmoticonToolBar = {
        let toolBar = WFEmoticonToolBar(frame: CGRectZero)
        return toolBar
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:  - 在layoutsubview里定义itemsize
    override func layoutSubviews() {
        super.layoutSubviews()
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = emoticonCollectionView.size
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
    
    }
    
    // MARK:  - 初始化视图
    private func setUpUI(){

        
        addSubview(emojiToolBar)
        addSubview(emoticonCollectionView)
        
        emojiToolBar.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self)
            make.leading.equalTo(self)
            make.bottom.equalTo(self)
            make.height.equalTo(30)
        }
        
        emojiToolBar.emojiBtnToolBarClosure = { [weak self] (type:WFEmoticonToolBarButtonType) in
            
            switch type {
            case .Normal:
                print("normal")
                
                 self!.scrowToFirstItemWithSection(0)
            case .Emoji:
                print("emoji")
                
                self!.scrowToFirstItemWithSection(1)
            case .LXH:
                print("lxh")
                
                self!.scrowToFirstItemWithSection(2)
            }

        }
        
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.trailing.equalTo(self)
            make.leading.equalTo(self)
            make.bottom.equalTo(emojiToolBar.snp_top)
        }
        
    }
    private func scrowToFirstItemWithSection(sectionNum:Int){
        let indexPath = NSIndexPath(forItem: 0, inSection: sectionNum)
        
        self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .CenteredHorizontally, animated: false)
    }
}



// MARK:  - 实现collectionview 的数据源和代理方法
extension WFEmoticonKeyBoard:UICollectionViewDelegate,UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return WFEmoticonTool.sharedTools.allEmoticonsArray.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WFEmoticonTool.sharedTools.allEmoticonsArray[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WFEmoticonKeyBoardCollectionViewCellID, forIndexPath: indexPath) as! WFEmoticonCVCell
        
        
//        cell.backgroundColor = RandomColor()
        cell.emoticons = WFEmoticonTool.sharedTools.allEmoticonsArray[indexPath.section][indexPath.item]
        
        return cell
        
        
    }
    

    
    
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
      let cells =   emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
            return firstCell.x < secondCell.x
        }
        
        if cells.count == 2 {
            let firstCell = cells.first!
            let secondCell = cells.last!
            
            
            let contentOffSetX = scrollView.contentOffset.x
            let firstContentOffSetX = abs(firstCell.x - contentOffSetX)
            let secondContentOffSetX = secondCell.x - contentOffSetX
            let indexPath: NSIndexPath
            if firstContentOffSetX < secondContentOffSetX {
                //
                print("还处于第一个")
                indexPath = emoticonCollectionView.indexPathForCell(firstCell)!
            } else {
                print("已经变成第二个了")
                indexPath = emoticonCollectionView.indexPathForCell(secondCell)!
            }
             emojiToolBar.clickOnBtnIcon(WFEmoticonToolBarButtonType(rawValue: indexPath.section)!)

        }
        
            }
    
    
}
